package com.thread.lock;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyReentrantLock {

	private Lock lock = new ReentrantLock();

	public void lockCurrentObject() {
		lock.lock();
		System.out.println(Thread.currentThread().getName() + "..start.." + "-----" + System.currentTimeMillis());
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(Thread.currentThread().getName() + "..end.." + "-----" + System.currentTimeMillis());
		lock.unlock();
	}

	public static void main(String[] args) {

		ExecutorService exec = Executors.newFixedThreadPool(3);
		final MyReentrantLock mrl = new MyReentrantLock();
		for (int i = 0; i < 5; i++) {
			exec.execute(new Runnable() {
				@Override
				public void run() {
					// 写在这里是错误的，因为lock同synchronized一样，无法锁住其它对象
					// MyReentrantLock mrl = new MyReentrantLock();
					mrl.lockCurrentObject();
				}
			});
		}
		exec.shutdown();

	}
}